﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;
using System.Runtime.Remoting.Channels;
using ForumServerIpcObjects;
using RemoteInteractiveForum.Domain;

namespace ForumServerProject
{
    class Server
    {
        static void Main(string[] args)
        {
            InitializeIpcChannel();

            Console.WriteLine();
            // Wait for the user input.
            Console.WriteLine("Type any key to exit IPC Server.");
            Console.ReadKey();
            Console.WriteLine("The server is exiting.");
        }

        private static void InitializeIpcChannel()
        {
            Console.WriteLine("IPC Server\n");

            IpcChannel ipcSvrChannel = new IpcChannel("localhost:9090");
            ChannelServices.RegisterChannel(ipcSvrChannel, false);
            Console.WriteLine("IPC Channel Name: {0}", ipcSvrChannel.ChannelName);
            Console.WriteLine("IPC Channel Priority: {0}.", ipcSvrChannel.ChannelPriority);
            ChannelDataStore channelData = (ChannelDataStore)ipcSvrChannel.ChannelData;
            foreach (string uri in channelData.ChannelUris)
            {
                Console.WriteLine("Channel URI is {0}.", uri);
            }
            Console.WriteLine();
            // Expose an object for remote calls.
            RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(InteractiveForum), "InteractiveForum",
                System.Runtime.Remoting.WellKnownObjectMode.Singleton);
            // Parse the channel's URI.
            string[] urls = ipcSvrChannel.GetUrlsForUri("InteractiveForum");
            if (urls.Length > 0)
            {
                string objectUrl = urls[0];
                string objectUri;
                string channelUri = ipcSvrChannel.Parse(objectUrl, out objectUri);
                Console.WriteLine("The object URI is {0}.", objectUri);
                Console.WriteLine("The channel URI is {0}.", channelUri);
                Console.WriteLine("The object URL is {0}.", objectUrl);
            }
        }
    }
}

